/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.net.ecode.common.utils.IdGen;
import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.common.persistence.TreeEntity;
import cn.net.ecode.modules.sys.entity.UserDict;
import cn.net.ecode.modules.sys.service.UserDictService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 用户字典管理Controller
 * @author ThinkGem
 * @version 2015-07-27
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/userDict")
public class UserDictController extends BaseController {

	@Autowired
	private UserDictService userDictService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public UserDict get(String dictCode, boolean isNewRecord) {
		return userDictService.get(dictCode, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("sys:userDict:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserDict userDict, Model model) {
		model.addAttribute("userDict", userDict);
		return "modules/sys/userDictList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("sys:userDict:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public List<UserDict> listData(UserDict userDict) {
		if (StringUtils.isBlank(userDict.getParentCode())) {
			userDict.setParentCode(UserDict.ROOT_CODE);
		}
		List<UserDict> list = null;
		if (StringUtils.isNotBlank(userDict.getDictCode())){
			list = Lists.newArrayList(get(userDict.getDictCode(), false));
		}else{
			list = userDictService.findList(userDict);
		}
		return list;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sys:userDict:view")
	@RequestMapping(value = "form")
	public String form(UserDict userDict, Model model) {
		if (userDict.getParent() != null && StringUtils.isNotBlank(userDict.getParent().getDictCode())) {
			userDict.setParent(userDictService.get(userDict.getParent().getDictCode()));
		} else {
			userDict.setParentCode(TreeEntity.ROOT_CODE);
		}
		if (StringUtils.isBlank(userDict.getDictCode())) {
			userDict.setSorts(userDictService.getMaxSort(userDict) + 30);
			// int size=userDictService.getMaxSize(userDict) ;
			// userDict.setDictCode(userDict.getParent().getDictCode() + StringUtils.leftPad(String.valueOf(size > 0 ? size+1 : 1), 3, "0"));
			userDict.setDictCode(IdGen.nextId());	
			userDict.setIsNewRecord(true);
		}
		model.addAttribute("userDict", userDict);
		return "modules/sys/userDictForm";		
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("sys:userDict:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(UserDict userDict) {
		if(Global.isDemoMode()){
			return renderResult(Global.FALSE, "演示模式，不允许操作！");
		}
		StringBuilder builder = new StringBuilder();
		if (!beanValidator(builder, userDict)){
			return renderResult(Global.FALSE, builder.toString());
		}
		userDictService.save(userDict);
		return renderResult(Global.TRUE, "保存用户字典成功");		
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("sys:userDict:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String delete(UserDict userDict) {
		userDictService.delete(userDict);
		return renderResult(Global.TRUE, "删除用户字典成功！");
	}
	
	/**
	 * 停用
	 */
	@RequiresPermissions("sys:userDict:edit")
	@ResponseBody
	@RequestMapping(value = "disable")
	public String disable(UserDict userDict) {
		if(Global.isDemoMode()){
			return renderResult(Global.FALSE, "演示模式，不允许操作！");
		}
		
		List<UserDict> userDictList = userDictService.getEnabledList(userDict);
		if(userDictList!=null&&userDictList.size()>0){
			return renderResult(Global.FALSE, "用户字典包含未停用的，请先停用子节点！");
		}
		userDictService.disable(userDict);
		return renderResult(Global.TRUE, "停用用户字典成功");
	}
	
	/**
	 * 按组停用
	 */
	@RequiresPermissions("sys:userDict:edit")
	@ResponseBody
	@RequestMapping(value = "disableByGroup")
	public String disableByGroup(UserDict userDict) {
		if(Global.isDemoMode()){
			return renderResult(Global.FALSE, "演示模式，不允许操作！");
		}
		userDictService.disableByGroup(userDict);
		return renderResult(Global.TRUE, "停用用户字典及子成功");
	}
	
	/**
	 * 启用
	 */
	@RequiresPermissions("sys:userDict:edit")
	@ResponseBody
	@RequestMapping(value = "enable")
	public String enable(UserDict userDict) {
		if(Global.isDemoMode()){
			return renderResult(Global.FALSE, "演示模式，不允许操作！");
		}
		userDictService.enable(userDict);
		return renderResult(Global.TRUE, "启用用户字典成功");
	}
	
	/**
	 * 按组启用
	 */
	@RequiresPermissions("sys:userDict:edit")
	@ResponseBody
	@RequestMapping(value = "enableByGroup")
	public String enableByGroup(UserDict userDict) {
		if(Global.isDemoMode()){
			return renderResult(Global.FALSE, "演示模式，不允许操作！");
		}
		userDictService.enableByGroup(userDict);
		return renderResult(Global.TRUE, "启用用户字典及子成功");
	}

	/**
	 * 获取结构数据。
	 * @param extCode 排除的ID
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(String extCode, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<UserDict> list = userDictService.findAllList(new UserDict());
		for (int i=0; i<list.size(); i++){
			UserDict e = list.get(i);
			if ((StringUtils.isBlank(extCode) || (extCode!=null && !extCode.equals(e.getDictCode()) && e.getParentCodes().indexOf(","+extCode+",")==-1))
					&&(e.getStatus().equals("0"))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getDictCode());
				map.put("pId", e.getParentCode());
				map.put("pIds", e.getParentCodes());
				map.put("name", e.getDictName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	/**
	 * 树结构数据修复
	 * @return
	 */
	@RequiresPermissions("sys:userDict:edit")
	@ResponseBody
	@RequestMapping(value = "dataFix")
	public String dataFix(){
		userDictService.updateFixParentCodes();
		return renderResult(Global.TRUE, "数据修复成功");
	}
	
}